<template>
  <div class="pageDetail">
    <el-form
      ref="formRef"
      class="form_item_w2"
      :model="pageForm"
      :rules="rules"
      label-width="120px"
    >
      <el-form-item label="学生姓名" prop="studentName">
        <el-input v-model="pageForm.studentName" placeholder="请输入学生姓名" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="学号" prop="studentNo">
        <el-input v-model="pageForm.studentNo" placeholder="请输入学号" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="所在班级" prop="classId">
        <TheDeptSelect v-model="pageForm.classId" :modelValue="pageForm.classId" :deptType="41" placeholder="请选择所属班级"  :disabled="disabled" @update:modelValue="pageForm.classId = $event"/>
      </el-form-item>
      <el-form-item label="培养类型" prop="educationTypeId">
        <el-select v-model="pageForm.educationTypeId" placeholder="请输入培养类型" :disabled="disabled">
          <el-option 
            v-for="(dictItem,index) in course_nature_type" 
            :label="dictItem.dictLabel" 
            :value="dictItem.dictCode"
            :key="index" 
            ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="培养形式" prop="educationModeId">
        <el-select v-model="pageForm.educationModeId" placeholder="请输入培养形式" :disabled="disabled">
          <el-option 
            v-for="(dictItem,index) in course_nature_type" 
            :label="dictItem.dictLabel" 
            :value="dictItem.dictCode"
            :key="index" 
            ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="指导教师" prop="instructorId">
        <el-select
          v-model="pageForm.instructorId"
          filterable
          remote
          reserve-keyword
          placeholder="请输入关键词"
          :remote-method="remoteMethod"
          :loading="loading">
          <el-option
            v-for="item in userList"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="结课时间" prop="endDate">
        <el-date-picker
          v-model="pageForm.endDate"
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
          :disabled="disabled"
          >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="开课时间" prop="startDate">
        <el-date-picker
          v-model="pageForm.startDate"
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
          :disabled="disabled"
          >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="指导内容" prop="guidanceContent" class="item_w1">
        <el-input type="textarea" v-model="pageForm.guidanceContent" placeholder="请输入指导内容" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="获奖情况" prop="awards" class="item_w1">
        <el-input type="textarea" v-model="pageForm.awards" placeholder="请输入获奖情况" :disabled="disabled"></el-input>
      </el-form-item>
       <el-form-item label="学生作品" prop="studentWorkFileId" class="item_w1">
        <FileUpload v-model="pageForm.studentWorkFileId" :fileInfo="pageForm.studentWorkFileList" :disabled="disabled"/>
      </el-form-item>
    </el-form>
    <div class="pageFooterBtn">
      <el-button @click="$router.back()">返回</el-button>
      <el-button type="primary" @click="handleSubmit()" v-if="['add','edit'].includes(action)" :loading="loading">提交</el-button>
    </div>
  </div>
</template>
<script>
import { rules } from './data'
import { useDict } from '@/utils/dict/useDict'
import { studentEducationAddApi, studentEducationEditApi, studentEducationByIdApi } from '@/api/teachingResearchReform/studentEducation'
import { listUser } from "@/api/system/user";
import FileUpload from "@/components/FileUpload";
import TheDeptSelect from '@/componentsView/TheDeptSelect.vue';

export default {
  name: 'StudentEducationDetail',
  components: { TheDeptSelect, FileUpload },
  data() {
    return {
      rules,
      userName: "",
      userList: [],
      pageForm: {},
      loading:false,
      disabled:false,
      course_nature_type: useDict('course_nature_type'),
      studentId: this.$route.query.studentId,
      action: this.$route.query.action
    }
  },

  async created() {
    if (this.studentId) this.getCoursePlanById()
  },
  methods: {
    async handleSubmit() {
      const pureNumberRegex = /^[+-]?(\d+(\.\d*)?|\.\d+)$/;
      if (pureNumberRegex.test(this.pageForm.studentName)) {
        this.$message.error('学生姓名不能为纯数字')
        return
      }
      try {
        await this.$refs.formRef.validate()
        this.loading = true
        let res = {}
        if (this.studentId) {
          delete this.pageForm.fileInfo
          res = await studentEducationEditApi(this.pageForm)
        }else{
          res = await studentEducationAddApi(this.pageForm)
        }
        if (res.code === 200) {
          this.$message.success(res.msg)
          this.$router.back()
        } else {
          this.$message.error(res.msg || '操作失败')
        }
      } catch (error) {
        this.$message.error('操作失败')
      } finally {
        this.loading = false
      }
    },

    async getCoursePlanById(){
      if (this.action === 'look') this.disabled = true
      studentEducationByIdApi(this.studentId).then(res => {
        if (res.code === 200) {
          this.pageForm = res.data
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    remoteMethod(val){
      let params = { userName:val, pageNum:1 ,pageSize:10}
      listUser(params).then((response) => {
        if (response.code == 200) {
          this.userList = response.rows
        }else{
          this.$message.error(response.msg);
        }
      });
    }
  },
}
</script>
<style scoped lang="scss">
::v-deep .el-select {
	width: 100% !important;
}
::v-deep .el-date-editor{
  width: 100% !important;
}
::v-deep .el-input-number{
  width: 100% !important;
}
</style>